

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/blog/img/fluid.png">
  <link rel="icon" href="/blog/images/base.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="Airfe">
  <meta name="keywords" content="">
  
    <meta name="description" content="本文介绍了Vue-Router相关知识。">
<meta property="og:type" content="article">
<meta property="og:title" content="Vue-Router">
<meta property="og:url" content="https://airfe.gitee.io/blog/2023/06/23/Vue%E8%B7%AF%E7%94%B1/index.html">
<meta property="og:site_name" content="Airfe的博客">
<meta property="og:description" content="本文介绍了Vue-Router相关知识。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://markdown-1307246189.cos.ap-chongqing.myqcloud.com/pageImage/Vue%E8%B7%AF%E7%94%B1.png">
<meta property="article:published_time" content="2023-06-23T00:00:00.000Z">
<meta property="article:modified_time" content="2024-02-26T12:23:22.413Z">
<meta property="article:author" content="Airfe">
<meta property="article:tag" content="Vue">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://markdown-1307246189.cos.ap-chongqing.myqcloud.com/pageImage/Vue%E8%B7%AF%E7%94%B1.png">
  
  
  
    <meta name="referrer" content="no-referrer" />
  
  <title>Vue-Router - Airfe的博客</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/blog/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/blog/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/blog/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"airfe.gitee.io","root":"/blog/","version":"1.9.4","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":true,"follow_dnt":true,"baidu":"699a5d71ac7179b23874a59b0666a6f6","google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":"RW68dMkEpeIOJJiBr1lPkhOS-gzGzoHsz","app_key":"P5fzMTGSJIoTaGvmgzsUHeNZ","server_url":"https://rw68dmke.lc-cn-n1-shared.com","path":"window.location.pathname","ignore_local":false}},"search_path":"/blog/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/blog/js/utils.js" ></script>
  <script  src="/blog/js/color-schema.js" ></script>
  

  
    <!-- Baidu Analytics -->
    <script async>
      if (!Fluid.ctx.dnt) {
        var _hmt = _hmt || [];
        (function() {
          var hm = document.createElement("script");
          hm.src = "https://hm.baidu.com/hm.js?699a5d71ac7179b23874a59b0666a6f6";
          var s = document.getElementsByTagName("script")[0];
          s.parentNode.insertBefore(hm, s);
        })();
      }
    </script>
  

  

  

  

  

  

  
    
  



  
<meta name="generator" content="Hexo 6.3.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/blog/">
      <strong>Airfe</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/">
                <i class="iconfont icon-home-fill"></i>
                <span>首页</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/archives/">
                <i class="iconfont icon-archive-fill"></i>
                <span>归档</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/categories/">
                <i class="iconfont icon-category-fill"></i>
                <span>分类</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/tags/">
                <i class="iconfont icon-tags-fill"></i>
                <span>标签</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/about/">
                <i class="iconfont icon-user-fill"></i>
                <span>关于</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/links/">
                <i class="iconfont icon-link-fill"></i>
                <span>友链</span>
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              <i class="iconfont icon-search"></i>
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">
              <i class="iconfont icon-dark" id="color-toggle-icon"></i>
            </a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/blog/images/bg.jpg') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="Vue-Router"></span>
          
        </div>

        
          
  <div class="mt-3">
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2023-06-23 08:00" pubdate>
          2023年06月23日
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          6.5k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          54 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">Vue-Router</h1>
            
            
              <div class="markdown-body">
                
                <h1 id="Vue-Router"><a href="#Vue-Router" class="headerlink" title="Vue-Router"></a>Vue-Router</h1><h2 id="1-1-相关理解"><a href="#1-1-相关理解" class="headerlink" title="1.1 相关理解"></a>1.1 相关理解</h2><h3 id="1-1-1-vue-router-的理解"><a href="#1-1-1-vue-router-的理解" class="headerlink" title="1.1.1 vue-router 的理解"></a>1.1.1 vue-router 的理解</h3><p><code>vue</code>的一个插件库，专门用来实现<code>SPA</code>（单页面跳转）应用。路由器检测到页面url变化就匹配路由，展示不同的组件。  </p>
<h3 id="1-1-2-路由的理解"><a href="#1-1-2-路由的理解" class="headerlink" title="1.1.2 路由的理解"></a>1.1.2 路由的理解</h3><p>1.什么是路由? </p>
<ul>
<li>一个路由就是一组映射关系（key - value）</li>
<li><code>key</code>为路径，<code>value</code>可能是<code>function</code>或<code>component</code></li>
</ul>
<p>2.路由分类</p>
<ul>
<li><p>后端路由 </p>
<ul>
<li>理解：<code>value</code>是<code>function</code>，于处理客户端提交的请求</li>
<li>工作过程：服务器接收到一个请求时，根据请求路径找到匹配的函数来处理请求，返回响应数据</li>
</ul>
</li>
<li><p>前端路由</p>
<ul>
<li>理解：<code>value</code>是<code>component</code>，用于展示页面内容</li>
<li>工作过程：当浏览器的路径改变时，对应的组件就会显示</li>
</ul>
</li>
</ul>
<h3 id="x3D-x3D-1-1-3-路由器的两种工作模式-x3D-x3D"><a href="#x3D-x3D-1-1-3-路由器的两种工作模式-x3D-x3D" class="headerlink" title="&#x3D;&#x3D;1.1.3 路由器的两种工作模式&#x3D;&#x3D;"></a>&#x3D;&#x3D;1.1.3 路由器的两种工作模式&#x3D;&#x3D;</h3><p>一个url带#就是hash模式，不带#号就是history模式。</p>
<blockquote>
<p>从#开始一直到结束的值都是hash值，不会随着http请求发送给服务器，只是前端路由处理。</p>
</blockquote>
<ul>
<li><code>hash</code>值不会包含在<code>http</code>请求中，即刷新不会被当成请求发给服务器。兼容性较好。</li>
<li><code>history</code>模式地址干净美观，但兼容性较差，需要后端处理识别url是前端路由跳转还是请求后端资源。</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">const</span> router = <span class="hljs-keyword">new</span> <span class="hljs-title class_">VueRouter</span>(&#123;<br>    <span class="hljs-attr">mode</span>: <span class="hljs-string">&#x27;hash&#x27;</span>,<br>    <span class="hljs-attr">routes</span>:[...]<br>&#125;)<br></code></pre></td></tr></table></figure>

<p>Express服务器如何解决history带来的问题：使用connect-history-api-fallback中间件</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">const</span> express = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;express&#x27;</span>)<br><span class="hljs-comment">// 1. npm i connect-history-api-fallback</span><br><span class="hljs-keyword">const</span> history = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;connect-history-api-fallback&#x27;</span>)<br><span class="hljs-keyword">const</span> app = <span class="hljs-title function_">express</span>()<br><br><span class="hljs-comment">// 2. 使用connect-history-api-fallback中间件</span><br>app.<span class="hljs-title function_">use</span>(<span class="hljs-title function_">history</span>())<br><span class="hljs-comment">// 3. static下是前端打包后的dist文件，此时打开该服务器才可以访问前端资源</span><br>app.<span class="hljs-title function_">use</span>(express.<span class="hljs-title function_">static</span>(_dirname+<span class="hljs-string">&#x27;/static&#x27;</span>))<br><br>app.<span class="hljs-title function_">listen</span>(<span class="hljs-number">80</span>, <span class="hljs-function">() =&gt;</span> &#123;<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;express server running at http://127.0.0.1&#x27;</span>)&#125;)<br></code></pre></td></tr></table></figure>

<h2 id="1-2-基本路由"><a href="#1-2-基本路由" class="headerlink" title="1.2 基本路由"></a>1.2 基本路由</h2><ol>
<li><p>安装<code>vue-router</code>：<code>npm  i  vue-router</code></p>
</li>
<li><p>在main.js中引入VueRouter并应用插件，编写<code>router</code>配置项</p>
</li>
</ol>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-comment">// 引入VueRouter路由器</span><br><span class="hljs-keyword">import</span> <span class="hljs-title class_">VueRouter</span> <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;vue-router&#x27;</span><br><span class="hljs-comment">// 引入组件</span><br><span class="hljs-keyword">import</span> <span class="hljs-title class_">About</span> <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;../pages/About&#x27;</span><br><span class="hljs-keyword">import</span> <span class="hljs-title class_">Home</span> <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;../pages/Home&#x27;</span><br><br><span class="hljs-comment">// 应用插件</span><br><span class="hljs-title class_">Vue</span>.<span class="hljs-title function_">use</span>(<span class="hljs-title class_">VueRouter</span>)<br><br><span class="hljs-comment">// 创建router路由器实例对象，去管理一组一组的路由规则</span><br><span class="hljs-keyword">const</span> router = <span class="hljs-keyword">new</span> <span class="hljs-title class_">VueRouter</span> (&#123;<br>     <span class="hljs-attr">routes</span>: [<br>        &#123;<br>            <span class="hljs-attr">path</span>: <span class="hljs-string">&#x27;/about&#x27;</span>,<br>            <span class="hljs-attr">component</span>: <span class="hljs-title class_">About</span>,<br>        &#125;,<br>        &#123;<br>            <span class="hljs-attr">path</span>: <span class="hljs-string">&#x27;/home&#x27;</span>,<br>            <span class="hljs-attr">component</span>: <span class="hljs-title class_">Home</span><br>        &#125;, <br>&#125;)<br> <br><br><span class="hljs-comment">// 可以识别路由,即根据url变化</span><br><span class="hljs-keyword">new</span> <span class="hljs-title class_">Vue</span>(&#123;<br>  <span class="hljs-attr">render</span>: <span class="hljs-function"><span class="hljs-params">h</span> =&gt;</span> <span class="hljs-title function_">h</span>(<span class="hljs-title class_">App</span>),<br>  <span class="hljs-attr">router</span>: router  <span class="hljs-comment">// 引入并应用了VueRouter插件以后就可以写这个配置项</span><br>&#125;).$mount(<span class="hljs-string">&#x27;#app&#x27;</span>)<br></code></pre></td></tr></table></figure>

<blockquote>
<p><code>router</code>配置项可以单独建一个router的文件夹在index.js里面写，然后再在main.js中导入即可</p>
</blockquote>
<ol start="3">
<li>在template中配置url如何改变，以及变化的组件在哪显示</li>
</ol>
<ul>
<li>在template中使用<router-link>改变url  相当于<a>标签</a></router-link></li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-comment">//active-class= &quot;active&quot;，该元素被点击时实现active类的样式</span><br>&lt;router-link <span class="hljs-keyword">class</span>=<span class="hljs-string">&quot;list-group-item &quot;</span> active-<span class="hljs-keyword">class</span>=<span class="hljs-string">&quot;active&quot;</span> to=<span class="hljs-string">&quot;/about&quot;</span>&gt;<span class="hljs-title class_">About</span>&lt;/router-link&gt;<br><span class="language-xml"><span class="hljs-tag">&lt;<span class="hljs-name">router-link</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;list-group-item&quot;</span> <span class="hljs-attr">active-class</span>=<span class="hljs-string">&quot;active&quot;</span> <span class="hljs-attr">to</span>=<span class="hljs-string">&quot;/home&quot;</span>&gt;</span>Home<span class="hljs-tag">&lt;/<span class="hljs-name">router-link</span>&gt;</span></span><br></code></pre></td></tr></table></figure>

<ul>
<li>指定展示位 <router-view></router-view></li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs js">&lt;router-view&gt;&lt;/router-view&gt;<br><br><span class="hljs-comment">// 如果需要缓存路由组件，使不看这个组件时该组件不被销毁，可以用keep-alive</span><br><span class="language-xml"><span class="hljs-tag">&lt;<span class="hljs-name">keep-alive</span> <span class="hljs-attr">include</span>=<span class="hljs-string">&quot;News&quot;</span>&gt;</span></span><br><span class="language-xml">    <span class="hljs-tag">&lt;<span class="hljs-name">router-view</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">router-view</span>&gt;</span></span><br><span class="language-xml"><span class="hljs-tag">&lt;/<span class="hljs-name">keep-alive</span>&gt;</span></span><br><br><span class="language-xml"><span class="hljs-tag">&lt;<span class="hljs-name">keep-alive</span> <span class="hljs-attr">:include</span>=<span class="hljs-string">&quot;[&#x27;News&#x27;,&#x27;Message&#x27;]&quot;</span>&gt;</span></span><br><span class="language-xml">    <span class="hljs-tag">&lt;<span class="hljs-name">router-view</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">router-view</span>&gt;</span></span><br><span class="language-xml"><span class="hljs-tag">&lt;/<span class="hljs-name">keep-alive</span>&gt;</span></span><br></code></pre></td></tr></table></figure>

<blockquote>
<p><router-link>有push和replace属性来控制url的历史记录仪，默认开启的是push模式（栈先进后出），想要无痕模式的效果就写replace（每次干掉前一条，替代成为前一条）</router-link></p>
</blockquote>
<h2 id="1-3-路由点击时样式"><a href="#1-3-路由点击时样式" class="headerlink" title="1.3 路由点击时样式"></a>1.3 路由点击时样式</h2><ol>
<li>active-class</li>
</ol>
<p>在router-link中写入active-class</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs js">&lt;router-link to=<span class="hljs-string">&quot;/home&quot;</span> active-<span class="hljs-keyword">class</span>=<span class="hljs-string">&quot;active&quot;</span>&gt;首页&lt;/router-link&gt;<br></code></pre></td></tr></table></figure>

<p>由于active-class选择样式时根据路由中的路径去从左往右匹配，然后显示，前面的路径相同时便都可以匹配到。故最好在router-link中写入&#x3D;&#x3D;<strong>exact</strong>&#x3D;&#x3D;。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs js">&lt;router-link to=<span class="hljs-string">&quot;/home&quot;</span> active-<span class="hljs-keyword">class</span>=<span class="hljs-string">&quot;active&quot;</span> exact&gt;首页&lt;/router-link&gt;<br></code></pre></td></tr></table></figure>

<p>再在style中写样式，active是自命名的，也可以取为其他名。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs js">.<span class="hljs-property">active</span> &#123;<br>    border-<span class="hljs-attr">bottom</span>: 2px solid #3a92e4;<br>&#125;<br></code></pre></td></tr></table></figure>

<ol start="2">
<li>router-link-active</li>
</ol>
<p>直接使用样式</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs js">.<span class="hljs-property">router</span>-link-active &#123;<br>  span &#123;<br>    border-<span class="hljs-attr">bottom</span>: 2px solid #3a92e4;<br>  &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>若有多个路由样式不一样，则用类.router-link-active 分别控制</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs js">&lt;router-link to=<span class="hljs-string">&quot;/home&quot;</span> <span class="hljs-keyword">class</span>=<span class="hljs-string">&quot;menu-home&quot;</span>&gt;首页&lt;/router-link&gt;<br><span class="language-xml"><span class="hljs-tag">&lt;<span class="hljs-name">router-link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">&quot;/my&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;menu-my&quot;</span>&gt;</span>首页<span class="hljs-tag">&lt;/<span class="hljs-name">router-link</span>&gt;</span></span><br><br>.<span class="hljs-property">menu</span>-home.<span class="hljs-property">router</span>-link-active &#123;<br>   border-<span class="hljs-attr">bottom</span>: 2px solid #3a92e4;<br>&#125;<br><br>.<span class="hljs-property">menu</span>-my.<span class="hljs-property">router</span>-link-active &#123;<br>   border-<span class="hljs-attr">bottom</span>: 2px solid #ef4238;<br>&#125;<br></code></pre></td></tr></table></figure>

<h2 id="1-4-嵌套路由"><a href="#1-4-嵌套路由" class="headerlink" title="1.4 嵌套路由"></a>1.4 嵌套路由</h2><p>其他不变只是配置项用children</p>
<ol>
<li>编写<code>router</code>配置项</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">const</span> router = <span class="hljs-keyword">new</span> <span class="hljs-title class_">VueRouter</span> (&#123;<br>     <span class="hljs-attr">routes</span>: [<br>        &#123;<br>            <span class="hljs-attr">path</span>: <span class="hljs-string">&#x27;/about&#x27;</span>,<br>            <span class="hljs-attr">component</span>: <span class="hljs-title class_">About</span>,<br>        &#125;,<br>        &#123;<br>            <span class="hljs-attr">path</span>: <span class="hljs-string">&#x27;/home&#x27;</span>,<br>            <span class="hljs-attr">component</span>: <span class="hljs-title class_">Home</span>,<br>            <span class="hljs-attr">children</span>:[<br>               &#123;<br>                   <span class="hljs-attr">path</span>:<span class="hljs-string">&#x27;news&#x27;</span>,  <span class="hljs-comment">//此处一定不要写/news</span><br>                   <span class="hljs-attr">component</span>:<span class="hljs-title class_">News</span><br>               &#125; <br>            ]<br>        &#125;, <br>&#125;)<br></code></pre></td></tr></table></figure>

<ol start="2">
<li>在template中配置url如何改变，写完整路径</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs js">&lt;router-link <span class="hljs-keyword">class</span>=<span class="hljs-string">&quot;list-group-item&quot;</span> active-<span class="hljs-keyword">class</span>=<span class="hljs-string">&quot;active&quot;</span> to=<span class="hljs-string">&quot;/home/news&quot;</span>&gt;<span class="hljs-title class_">Home</span>&lt;/router-link&gt;<br></code></pre></td></tr></table></figure>

<p><strong>总结：<code>template</code>中首先调用<router-link>改变url ，路由器实例检测到url变化会开始检测配置的路由，有符合的就找到应该替换的组件准备展示，由<code>template</code>中<code>&lt;router-view&gt;</code>指定该组件展示位置。</router-link></strong></p>
<blockquote>
<p>$router是VueRouter的一个实例，他包含了所有的路由，包括路由的跳转方法，钩子函数等，也包含一些子对象（例如history）。</p>
<p>$route是一个跳转的路由对象（路由信息对象），每一个路由都会有一个自己的$route对象，里面存储着自己的路由信息（包含参数信息）。</p>
</blockquote>
<h2 id="1-5-路由传参"><a href="#1-5-路由传参" class="headerlink" title="1.5 路由传参"></a>1.5 路由传参</h2><p>如果需求是点击不同按钮跳转后的页面结构类似，只是内容不同，比如点不同文章，进去是不同文章的详情页。就需要对跳转路由传参。</p>
<h3 id="query参数"><a href="#query参数" class="headerlink" title="query参数"></a>query参数</h3><p>写在<code>template</code>中，用<code>router-link</code>代替a改变url</p>
<p>传递参数</p>
 <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs js">&lt;router-link :to = <span class="hljs-string">&quot;&#123;</span><br><span class="hljs-string">	path:&#x27;/home/message/detail&#x27;,</span><br><span class="hljs-string">    query:&#123;</span><br><span class="hljs-string">        id: m.id,</span><br><span class="hljs-string">        title: m.title</span><br><span class="hljs-string">    &#125;</span><br><span class="hljs-string">&#125;&quot;</span>&gt; 跳转 &lt;/router-link&gt;<br></code></pre></td></tr></table></figure>

<p>接收参数</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs js">$route.<span class="hljs-property">query</span>.<span class="hljs-property">id</span><br>$route.<span class="hljs-property">query</span>.<span class="hljs-property">title</span><br></code></pre></td></tr></table></figure>

<h3 id="命名路由"><a href="#命名路由" class="headerlink" title="命名路由"></a>命名路由</h3><p>作用：简化路由跳转</p>
<p>用name给路由命名</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">const</span> router = <span class="hljs-keyword">new</span> <span class="hljs-title class_">VueRouter</span> (&#123;<br>     <span class="hljs-attr">routes</span>: [<br>        &#123;<br>            <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;about&#x27;</span>,<br>            <span class="hljs-attr">path</span>: <span class="hljs-string">&#x27;/about&#x27;</span>,<br>            <span class="hljs-attr">component</span>: <span class="hljs-title class_">About</span>,<br>        &#125;,<br>&#125;)<br></code></pre></td></tr></table></figure>

<p>简化跳转</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs js">&lt;router-link :to = <span class="hljs-string">&quot;&#123;</span><br><span class="hljs-string">	name:&#x27;about&#x27;,</span><br><span class="hljs-string">    query:&#123;</span><br><span class="hljs-string">        id: m.id,</span><br><span class="hljs-string">        title: m.title</span><br><span class="hljs-string">    &#125;</span><br><span class="hljs-string">&#125;&quot;</span>&gt;<br></code></pre></td></tr></table></figure>

<h3 id="params参数"><a href="#params参数" class="headerlink" title="params参数"></a>params参数</h3><p>配置路由</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-attr">children</span>:[<br>    &#123;<br>        <span class="hljs-attr">name</span>:<span class="hljs-string">&#x27;xiangiqng&#x27;</span>,<br>        <span class="hljs-attr">path</span>:<span class="hljs-string">&#x27;detail/:id/:title&#x27;</span>, <span class="hljs-comment">//使用占位符声明接收params参数</span><br>        <span class="hljs-attr">component</span>:<span class="hljs-title class_">Detail</span><br>    &#125;<br>]<br></code></pre></td></tr></table></figure>

<p>传递参数</p>
<p>param写法中路径不能写path，只能写<code>name</code></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs js">&lt;router-link :to = <span class="hljs-string">&quot;&#123;</span><br><span class="hljs-string">	name:&#x27;about&#x27;,</span><br><span class="hljs-string">    params:&#123;</span><br><span class="hljs-string">        id: m.id,</span><br><span class="hljs-string">        title: m.title</span><br><span class="hljs-string">    &#125;</span><br><span class="hljs-string">&#125;&quot;</span>&gt;<br></code></pre></td></tr></table></figure>

<p>接收参数</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs js">$route.<span class="hljs-property">params</span>.<span class="hljs-property">id</span><br>$route.<span class="hljs-property">params</span>.<span class="hljs-property">title</span><br></code></pre></td></tr></table></figure>

<h2 id="1-6-编程式路由导航"><a href="#1-6-编程式路由导航" class="headerlink" title="1.6 编程式路由导航"></a>1.6 编程式路由导航</h2><p>不借助<router-link>实现路由的跳转，更灵活。</router-link></p>
<p>&#x3D;&#x3D;<code>this.$router.push(&#123;&#125;)  </code>        内传的对象与<code>&lt;router-link&gt;</code>中的<code>to</code>相同&#x3D;&#x3D;</p>
<p><code>this.$router.replace(&#123;&#125;)</code></p>
<p><code>this.$router.forward()</code>     前进</p>
<p><code>this.$router.back()</code>           后退</p>
<p><code>this.$router.go(n)</code>             可前进也可后退，n为正数前进n，负数后退</p>
<h2 id="1-7-路由守卫"><a href="#1-7-路由守卫" class="headerlink" title="1.7 路由守卫"></a>1.7 路由守卫</h2><p>作用：对路由进行权限控制</p>
<p>分类：全局守卫、独享守卫、组件内守卫</p>
<p>全局守卫</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">const</span> router = <span class="hljs-keyword">new</span> <span class="hljs-title class_">VueRouter</span> (&#123;<br>     <span class="hljs-attr">routes</span>: [<br>        &#123;<br>            <span class="hljs-attr">name</span>:<span class="hljs-string">&#x27;guanyu&#x27;</span>,<br>            <span class="hljs-attr">path</span>: <span class="hljs-string">&#x27;/about&#x27;</span>,<br>            <span class="hljs-attr">component</span>: <span class="hljs-title class_">About</span>,<br>            <span class="hljs-attr">meta</span>:&#123;<span class="hljs-attr">isAuth</span>:<span class="hljs-literal">true</span>,<span class="hljs-attr">title</span>:<span class="hljs-string">&#x27;消息&#x27;</span>&#125; <span class="hljs-comment">//是否需要权限校验</span><br>        &#125;<br>&#125;)<br></code></pre></td></tr></table></figure>

<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-comment">//全局前置路由守卫，初始化和每次路由切换之前被调用</span><br>router.<span class="hljs-title function_">beforeEach</span>(<span class="hljs-function">(<span class="hljs-params">to, <span class="hljs-keyword">from</span>, next</span>) =&gt;</span> &#123;<br>    <span class="hljs-comment">// if (to.path === &#x27;/home/news&#x27; || to.path === &#x27;/home/message&#x27;)</span><br>    <span class="hljs-comment">// if (to.name === &#x27;xinwen&#x27; || to.name === &#x27;xiaoxi&#x27;)</span><br>    <span class="hljs-keyword">if</span> (to.<span class="hljs-property">meta</span>.<span class="hljs-property">isAuth</span>)  <span class="hljs-comment">//判断是否需要鉴权</span><br>    &#123;<br>        <span class="hljs-keyword">if</span> (<span class="hljs-variable language_">localStorage</span>.<span class="hljs-title function_">getItem</span>(<span class="hljs-string">&#x27;school&#x27;</span>) === <span class="hljs-string">&#x27;school&#x27;</span>) &#123;<br>            <span class="hljs-title function_">next</span>()<br>        &#125; <span class="hljs-keyword">else</span> &#123;<br>            <span class="hljs-title function_">alert</span>(<span class="hljs-string">&#x27;学校名不对,无权限查看！&#x27;</span>)<br>        &#125;<br>    &#125;<br>    <span class="hljs-keyword">else</span> &#123;<br>        <span class="hljs-comment">// 放行 </span><br>        <span class="hljs-title function_">next</span>()<br>    &#125;<br>&#125;)<br><br><span class="hljs-comment">//全局后置路由守卫，初始化和每次路由切换之后被调用</span><br>router.<span class="hljs-title function_">afterEach</span>(<span class="hljs-function">(<span class="hljs-params">to, <span class="hljs-keyword">from</span></span>) =&gt;</span> &#123;<br>    <span class="hljs-variable language_">document</span>.<span class="hljs-property">title</span> = to.<span class="hljs-property">meta</span>.<span class="hljs-property">title</span> || <span class="hljs-string">&#x27;路由学习&#x27;</span><br>&#125;)<br></code></pre></td></tr></table></figure>

<p>独享守卫</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-comment">// beforeEnter和children同级 </span><br><span class="hljs-attr">beforeEnter</span>: <span class="hljs-function">(<span class="hljs-params">to, <span class="hljs-keyword">from</span>, next</span>) =&gt;</span> &#123;<br><span class="hljs-keyword">if</span> (to.<span class="hljs-property">meta</span>.<span class="hljs-property">isAuth</span>)  <span class="hljs-comment">//判断是否需要鉴权</span><br>&#123;<br>    <span class="hljs-keyword">if</span> (<span class="hljs-variable language_">localStorage</span>.<span class="hljs-title function_">getItem</span>(<span class="hljs-string">&#x27;school&#x27;</span>) === <span class="hljs-string">&#x27;school&#x27;</span>) &#123;<br>        <span class="hljs-title function_">next</span>()<br>    &#125; <span class="hljs-keyword">else</span> &#123;<br>        <span class="hljs-title function_">alert</span>(<span class="hljs-string">&#x27;学校名不对,无权限查看！&#x27;</span>)<br>    &#125;<br>&#125;<br><span class="hljs-keyword">else</span> &#123;<br>    <span class="hljs-title function_">next</span>()<br>&#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>组件内守卫</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-comment">//跟mounted()&#123;&#125;同级</span><br><br><span class="hljs-comment">//通过路由规则,进入该组件时被调用</span><br>beforeRouteEnter (to, <span class="hljs-keyword">from</span>, next) &#123; &#125;<br><span class="hljs-comment">//通过路由规则，离开该组件时被调用</span><br>beforeRouteLeave (to, <span class="hljs-keyword">from</span>, next) &#123; &#125;<br></code></pre></td></tr></table></figure>

<blockquote>
<p>后置守卫是切换完就调，组件内守卫beforeRouteLeave是离开该组件时调用</p>
</blockquote>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/blog/categories/%E5%89%8D%E7%AB%AF%E6%8A%80%E6%9C%AF%E6%A0%88/" class="category-chain-item">前端技术栈</a>
  
  
    <span>></span>
    
  <a href="/blog/categories/%E5%89%8D%E7%AB%AF%E6%8A%80%E6%9C%AF%E6%A0%88/%E6%A1%86%E6%9E%B6/" class="category-chain-item">框架</a>
  
  

  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/blog/tags/Vue/">#Vue</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>Vue-Router</div>
      <div>https://airfe.gitee.io/blog/2023/06/23/Vue路由/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>Airfe</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2023年6月23日</div>
        </div>
      
      
      
        <div class="license-meta-item">
          <div>许可协议</div>
          <div>
            
              
              
                <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
                  <span class="hint--top hint--rounded" aria-label="BY - 署名">
                    <i class="iconfont icon-by"></i>
                  </span>
                </a>
              
            
          </div>
        </div>
      
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/blog/2023/06/23/VueX/" title="VueX">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">VueX</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/blog/2023/06/22/Vue%E5%93%8D%E5%BA%94%E5%BC%8F%E5%8E%9F%E7%90%86/" title="Vue响应式原理">
                        <span class="hidden-mobile">Vue响应式原理</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
  
  
    <article id="comments" lazyload>
      
  <div id="valine"></div>
  <script type="text/javascript">
    Fluid.utils.loadComments('#valine', function() {
      Fluid.utils.createScript('https://lib.baomitu.com/valine/1.5.1/Valine.min.js', function() {
        var options = Object.assign(
          {"appId":"RW68dMkEpeIOJJiBr1lPkhOS-gzGzoHsz","appKey":"P5fzMTGSJIoTaGvmgzsUHeNZ","path":"window.location.pathname","placeholder":"发表你的看法吧~","avatar":"retro","meta":["nick","mail","link"],"requiredFields":[],"pageSize":10,"lang":"zh-CN","highlight":false,"recordIP":false,"serverURLs":"","emojiCDN":null,"emojiMaps":null,"enableQQ":false,"appid":"RW68dMkEpeIOJJiBr1lPkhOS-gzGzoHsz","appkey":"P5fzMTGSJIoTaGvmgzsUHeNZ","server_url":"https://rw68dmke.lc-cn-n1-shared.com"},
          {
            el: "#valine",
            path: window.location.pathname
          }
        )
        new Valine(options);
        Fluid.utils.waitElementVisible('#valine .vcontent', () => {
          var imgSelector = '#valine .vcontent img:not(.vemoji)';
          Fluid.plugins.imageCaption(imgSelector);
          Fluid.plugins.fancyBox(imgSelector);
        })
      });
    });
  </script>
  <noscript>Please enable JavaScript to view the comments</noscript>


    </article>
  


          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header">
    <i class="iconfont icon-list"></i>
    <span>目录</span>
  </p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
        <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> <div style="font-size: 0.85rem"> <span id="timeDate">载入天数</span> <span id="times">载入时分秒</span> <script src="js/duration.js"></script> </div> 
    </div>
  
  
    <div class="statistics">
  
  

  
    
      <span id="leancloud-site-pv-container" style="display: none">
        总访问量 
        <span id="leancloud-site-pv"></span>
         次
      </span>
    
    
      <span id="leancloud-site-uv-container" style="display: none">
        总访客数 
        <span id="leancloud-site-uv"></span>
         人
      </span>
    
    

  
</div>

  
  
    <!-- 备案信息 ICP for China -->
    <div class="beian">
  <span>
    <a href="http://beian.miit.gov.cn/" target="_blank" rel="nofollow noopener">
      蜀ICP备2023008943号
    </a>
  </span>
  
    
      <span>
        <a
          href="http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=https://beian.miit.gov.cn/"
          rel="nofollow noopener"
          class="beian-police"
          target="_blank"
        >
          
            <span style="visibility: hidden; width: 0">|</span>
            <img src="" srcset="/blog/img/loading.gif" lazyload data-original="/blog/img/police_beian.png" alt="police-icon"/>
          
          <span>京公网安备12345678号</span>
        </a>
      </span>
    
  
</div>

  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/blog/js/events.js" ></script>
<script  src="/blog/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/blog/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init(Object.assign({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      scrollSmooth    : true,
      includeTitleTags: true,
      headingsOffset  : -boardTop,
    }, CONFIG.toc));
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }

    Fluid.events.registerRefreshCallback(function() {
      if ('tocbot' in window) {
        tocbot.refresh();
        var toc = jQuery('#toc');
        if (toc.length === 0 || !tocbot) {
          return;
        }
        if (toc.find('.toc-list-item').length > 0) {
          toc.css('visibility', 'visible');
        }
      }
    });
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.11/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));

    Fluid.events.registerRefreshCallback(function() {
      if ('anchors' in window) {
        anchors.removeAll();
        var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
        var res = [];
        for (var item of el) {
          res.push('.markdown-body > ' + item.trim());
        }
        if (CONFIG.anchorjs.placement === 'left') {
          anchors.options.class = 'anchorjs-link-left';
        }
        anchors.add(res.join(', '));
      }
    });
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script defer src="/blog/js/leancloud.js" ></script>

  <script  src="/blog/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/blog/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>

        <style>
            [bg-lazy] {
                background-image: none !important;
                background-color: #eee !important;
            }
        </style>
        <script>
            window.imageLazyLoadSetting = {
                isSPA: false,
                preloadRatio: 1,
                processImages: null,
            };
        </script><script>window.addEventListener("load",function(){var t=/\.(gif|jpg|jpeg|tiff|png)$/i,r=/^data:image\/[a-z]+;base64,/;Array.prototype.slice.call(document.querySelectorAll("img[data-original]")).forEach(function(a){var e=a.parentNode;"A"===e.tagName&&(e.href.match(t)||e.href.match(r))&&(e.href=a.dataset.original)})});</script><script>!function(r){r.imageLazyLoadSetting.processImages=t;var e=r.imageLazyLoadSetting.isSPA,n=r.imageLazyLoadSetting.preloadRatio||1,c=a();function a(){var t=Array.prototype.slice.call(document.querySelectorAll("img[data-original]")),e=Array.prototype.slice.call(document.querySelectorAll("[bg-lazy]"));return t.concat(e)}function t(){e&&(c=a());for(var t,o=0;o<c.length;o++)0<=(t=(t=c[o]).getBoundingClientRect()).bottom&&0<=t.left&&t.top<=(r.innerHeight*n||document.documentElement.clientHeight*n)&&function(){var t,e,n,a,i=c[o];e=function(){c=c.filter(function(t){return i!==t}),r.imageLazyLoadSetting.onImageLoaded&&r.imageLazyLoadSetting.onImageLoaded(i)},(t=i).hasAttribute("bg-lazy")?(t.removeAttribute("bg-lazy"),e&&e()):(n=new Image,a=t.getAttribute("data-original"),n.onload=function(){t.src=a,t.removeAttribute("data-original"),e&&e()},t.src!==a&&(n.src=a))}()}function i(){clearTimeout(t.tId),t.tId=setTimeout(t,500)}t(),document.addEventListener("scroll",i),r.addEventListener("resize",i),r.addEventListener("orientationchange",i)}(this);</script></body>
</html>
